/*
 * Copyright (C) 2009 Rob Cash
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ScheduleChooser.java
 * 
 * Created on Jan 3, 2009 at 12:17:44 PM
 */

package org.robcash.apps.messagepopup.view.components;

import javax.swing.ComboBoxModel;
import org.robcash.apps.messagepopup.vo.Schedule;
import org.robcash.commons.swing.wizard.WizardComponent;

/**
 *
 * @author Rob
 */
public class ScheduleChooser extends WizardComponent {

    /**
     * Combo box model
     */
    private ComboBoxModel scheduleChooserModel;

    /** Creates new form ScheduleChooser */
    public ScheduleChooser() {
        scheduleChooserModel = new ScheduleChooserModel();
        initComponents();
    }

    /**
     * Set schedules
     */
    public void updateSchedules() {
        scheduleChooserModel = new ScheduleChooserModel();
        scheduleChooser.setModel(scheduleChooserModel);
    }

    /**
     * Get the selected schedule
     * @return Selected schedule or null if no schedule was selected
     */
    public Schedule getSelectedSchedule() {
        if ( newScheduleRadioButton.isSelected() ) {
            return null;
        }
        else {
            return (Schedule) scheduleChooser.getSelectedItem();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scheduleTypeButtonGroup = new javax.swing.ButtonGroup();
        instructions = new javax.swing.JLabel();
        existingScheduleRadioButton = new javax.swing.JRadioButton();
        newScheduleRadioButton = new javax.swing.JRadioButton();
        scheduleChooser = new javax.swing.JComboBox();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.robcash.apps.messagepopup.Main.class).getContext().getResourceMap(ScheduleChooser.class);
        instructions.setText(resourceMap.getString("instructions.text")); // NOI18N
        instructions.setName("instructions"); // NOI18N

        scheduleTypeButtonGroup.add(existingScheduleRadioButton);
        existingScheduleRadioButton.setSelected(true);
        existingScheduleRadioButton.setText(resourceMap.getString("existingScheduleRadioButton.text")); // NOI18N
        existingScheduleRadioButton.setName("existingScheduleRadioButton"); // NOI18N
        existingScheduleRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                scheduleChanged(evt);
            }
        });

        scheduleTypeButtonGroup.add(newScheduleRadioButton);
        newScheduleRadioButton.setText(resourceMap.getString("newScheduleRadioButton.text")); // NOI18N
        newScheduleRadioButton.setName("newScheduleRadioButton"); // NOI18N
        newScheduleRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                scheduleChanged(evt);
            }
        });

        scheduleChooser.setModel(scheduleChooserModel);
        scheduleChooser.setName("scheduleChooser"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(instructions)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(newScheduleRadioButton)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(existingScheduleRadioButton)
                                .addGap(18, 18, 18)
                                .addComponent(scheduleChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(110, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(instructions)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(existingScheduleRadioButton)
                    .addComponent(scheduleChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(newScheduleRadioButton)
                .addContainerGap(214, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void scheduleChanged(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scheduleChanged
        // Notify listeners that the schedule has changed
        fireChangeListenerStateChanged();
    }//GEN-LAST:event_scheduleChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton existingScheduleRadioButton;
    private javax.swing.JLabel instructions;
    private javax.swing.JRadioButton newScheduleRadioButton;
    private javax.swing.JComboBox scheduleChooser;
    private javax.swing.ButtonGroup scheduleTypeButtonGroup;
    // End of variables declaration//GEN-END:variables

}
